package com.sdp.wx.controller;


import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.wx.db.wx_repair_code.entity.WxRepairCode;
import com.sdp.wx.db.wx_repair_code.service.IWxRepairCodeService;
import com.sdp.wx.utils.LocalDateUtil;
import com.sdp.wx.utils.WxConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-16
 */
@RestController
@RequestMapping("/WxRepairCodeController")
public class WxRepairCodeController {
	
	@Autowired
	private IWxRepairCodeService iWxRepairCodeService;

	@Autowired
	private RedissonClient redissonClient;
	
	public String createRepairCode(String codeType,String codePrefix) {
		if(StringUtils.isEmpty(codeType)) {
			return null;
		}
		String repairCode = null;
		RLock lock = null;
		if(WxConstants.流水号类型_维修单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.维修单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材采购单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.采购单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材到货单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.到货单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材检验单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.检验单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材入库单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.入库单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材出库单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.出库单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材盘存单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.盘存单号REDIS锁名.getCode());
		}else if(WxConstants.流水号类型_器材领料单.getCode().equals(codeType)) {
			lock = redissonClient.getLock(WxConstants.领料单号REDIS锁名.getCode());
		}
		if(lock==null) {
			return null;
		}
		try {
			if (lock.tryLock(3, TimeUnit.SECONDS)) {
				repairCode = ApplicationContextUtils.getBean(WxRepairCodeController.class).getCode(codePrefix,codeType);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			lock.unlock();
		}
		
		return repairCode;
	}
	
	@Transactional(rollbackFor=Exception.class)
	public String getCode(String prefix,String codeType) {
		String nowMonthStr = LocalDateUtil.localDate2String(LocalDate.now(),"yyyy-MM");
		LocalDate nowMonth = LocalDateUtil.String2LocalDate(nowMonthStr+"-01");
		LambdaQueryWrapper<WxRepairCode> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(WxRepairCode::getRepairMonth, nowMonth);
		queryWrapper.eq(WxRepairCode::getCodeType, codeType);
		WxRepairCode wxRepairCode = iWxRepairCodeService.getOne(queryWrapper,false);
		if(StringUtils.isEmpty(wxRepairCode)) {
			wxRepairCode = new WxRepairCode();
			wxRepairCode.setRepairMonth(nowMonth);
			wxRepairCode.setRepairDate(LocalDate.now());
			wxRepairCode.setSerialNo(1);
			wxRepairCode.setCodeType(codeType);
			iWxRepairCodeService.save(wxRepairCode);
		}else {
			wxRepairCode.setSerialNo(wxRepairCode.getSerialNo()+1);
			iWxRepairCodeService.updateById(wxRepairCode);
		}
		String str = String.valueOf(wxRepairCode.getSerialNo());
		int len = str.length();
		if (len >= 4) {
			return str;
		}
		int rest = 4 - len;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < rest; i++) {
			sb.append('0');
		}
		sb.append(str);
		sb.insert(0, prefix+nowMonthStr.replace("-", "")+"-");
		return sb.toString();
	}
}

